﻿Namespace SiiabCamarasMovil
    Public Class VerCamarasController
        Inherits System.Web.Mvc.Controller

        Private repCamaras As CamarasRepositorio
        Private repVistas As VistasRepositorio
        Private Const MAXIMO_CAMARAS_PANTALLA As Integer = 4
        '
        ' GET: /VerCamaras
        <HttpGet()> _
        Function Index() As ActionResult
            Dim objList As New List(Of Camaras)
            Return View("~/Views/Sav/Monitoreo/VerCamaras/Index.vbhtml", objList)
        End Function


        <HttpGet()> _
        Function ListaVistas() As JsonResult
            Dim listVistas As New List(Of Vistas)
            Try
                If Request.IsAjaxRequest = True Then
                    repVistas = New VistasRepositorio
                    listVistas = repVistas.BuscarTodasVistas()
                End If
            Catch ex As Exception

            End Try
            Return Json(listVistas, JsonRequestBehavior.AllowGet)
        End Function

        <HttpGet()> _
        Function ListaCamaras(ByVal idRegion As String, ByVal idZona As String, ByVal idPoblacion As String) As JsonResult
            Dim listCamaras As New List(Of Camaras)
            Try
                If Request.IsAjaxRequest = True Then
                    repCamaras = New CamarasRepositorio
                    listCamaras = repCamaras.BuscarCamarasPorRegionZonaPob(CInt(idRegion), CInt(idZona), CInt(idPoblacion))
                    If Not System.Web.HttpContext.Current.Session("ListaCamaras") Is Nothing = True Then
                        System.Web.HttpContext.Current.Session.Remove("ListaCamaras")
                    End If
                    If Not System.Web.HttpContext.Current.Session("TotalBloques") Is Nothing = True Then
                        System.Web.HttpContext.Current.Session.Remove("TotalBloques")
                    End If
                    If Not System.Web.HttpContext.Current.Session("BloqueActual") Is Nothing = True Then
                        System.Web.HttpContext.Current.Session.Remove("BloqueActual")
                    End If
                    System.Web.HttpContext.Current.Session.Add("BloqueActual", 0)
                    System.Web.HttpContext.Current.Session.Add("TotalBloques", TotalBloques(listCamaras.Count))
                    System.Web.HttpContext.Current.Session.Add("ListaCamaras", listCamaras)

                End If
            Catch ex As Exception

            End Try
            Return Json(listCamaras, JsonRequestBehavior.AllowGet)
        End Function



        <OutputCache(NoStore:=True, Location:=OutputCacheLocation.Client, Duration:=3)> _
        <HttpPost()> _
        Function ActualizarCamaras() As PartialViewResult
            Dim listaCamarasSesion As New List(Of Camaras)
            Dim listaCamarasNueva As New List(Of Camaras)
            Dim intPosicionLista As Integer = 0
            Dim intActualBloque As Integer = 1
            Dim intTotalBloques As Integer = 1
            ModelState.Clear()

            Try
                If Request.IsAjaxRequest = True Then

                    If Not System.Web.HttpContext.Current.Session("TotalBloques") Is Nothing = True Then
                        intTotalBloques = CInt(System.Web.HttpContext.Current.Session("TotalBloques"))
                    End If
                    If Not System.Web.HttpContext.Current.Session("BloqueActual") Is Nothing = True Then
                        intActualBloque = CInt(System.Web.HttpContext.Current.Session("BloqueActual"))
                    End If
                    If intActualBloque >= intTotalBloques Then
                        intActualBloque = 0
                    End If

                    If intActualBloque > 0 Then
                        intPosicionLista = ((intActualBloque * MAXIMO_CAMARAS_PANTALLA))
                    End If

                    If Not System.Web.HttpContext.Current.Session("ListaCamaras") Is Nothing = True Then
                        listaCamarasSesion = CType(System.Web.HttpContext.Current.Session("ListaCamaras"), List(Of Camaras))
                    End If
                    If Not System.Web.HttpContext.Current.Session("BloqueActual") Is Nothing = True Then
                        System.Web.HttpContext.Current.Session.Remove("BloqueActual")
                    End If
                    System.Web.HttpContext.Current.Session.Add("BloqueActual", (intActualBloque + 1))
                    listaCamarasNueva = ObtenerListaCamarasBloque(listaCamarasSesion, intPosicionLista)
                End If

            Catch ex As Exception

            End Try
            Return PartialView("_PartialCamera", listaCamarasNueva)
        End Function



        Private Function TotalBloques(ByVal cantidadCamaras As Integer) As Integer

            Dim dblBloques As Double = 0
            Dim dblResiduo As Double = 0
            Dim dblMitadMaximoPantalla As Double = 0
            Dim intCantidadBloques As Integer = 0
            dblResiduo = cantidadCamaras Mod MAXIMO_CAMARAS_PANTALLA
            dblBloques = cantidadCamaras / MAXIMO_CAMARAS_PANTALLA
            dblMitadMaximoPantalla = MAXIMO_CAMARAS_PANTALLA / 2
            intCantidadBloques = dblBloques
            If dblResiduo > 0 Then
                If dblResiduo <= dblMitadMaximoPantalla Then
                    intCantidadBloques += 1
                End If
            End If

            Return intCantidadBloques
        End Function

        Private Function ObtenerListaCamarasBloque(ByVal listaCamarasSesion As List(Of Camaras), ByVal posicion As Integer) As List(Of Camaras)
            Dim listaCamaras As New List(Of Camaras)
            Dim intCantidad As Integer = 0
            Dim intContador As Integer = 0
            intCantidad = listaCamarasSesion.Count
            If intCantidad > 0 Then

                For contador As Integer = 0 To intCantidad - 1 Step 1
                    If listaCamarasSesion.Count <= MAXIMO_CAMARAS_PANTALLA Then
                        listaCamaras.Add(listaCamarasSesion.ElementAt(contador))
                    Else
                        If posicion <= contador AndAlso posicion <= intCantidad - 1 Then
                            If intContador < MAXIMO_CAMARAS_PANTALLA Then
                                listaCamaras.Add(listaCamarasSesion.ElementAt(contador))
                                intContador += 1
                            End If
                        End If
                    End If

      
                Next
            End If

            Return listaCamaras
        End Function


    End Class
End Namespace
